package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.INewsTeleplayRelaDao;
import com.magicode.cms.entiy.base.NewsTeleplayRela;
import com.magicode.cms.service.INewsTeleplayRelaService;


@Service("newsTeleplayRelaservice")
public class NewsTeleplayRelaServiceImpl extends BaseServiceImpl<NewsTeleplayRela,INewsTeleplayRelaDao>  implements INewsTeleplayRelaService{

	@Autowired
	@Qualifier("newsTeleplayRelaDao")
	private INewsTeleplayRelaDao newsTeleplayRelaDao;
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +NewsTeleplayRela.class.getName()+" "+entity_alias_name+" where 1 = 1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get(field_title)+"%'";
			}
			if(map.containsKey("attn")){
				hql = hql+" and "+entity_alias_name+".attn like '%"+map.get("attn")+"%'";
			}

			if(map.containsKey("authBeginTimeFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".authBeginTime,'"+sql_date_format+"') >= '"+map.get("authBeginTimeFrom")+"'";
			}
			if(map.containsKey("authBeginTimeTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".authBeginTime,'"+sql_date_format+"') <= '"+map.get("authBeginTimeTo")+"'";
			}
			if(map.containsKey("authEndDateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".authEndDate,'"+sql_date_format+"') >= '"+map.get("authEndDateFrom")+"'";
			}
			if(map.containsKey("authEndDateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".authEndDate,'"+sql_date_format+"') <= '"+map.get("authEndDateTo")+"'";
			}
		}
		
		return hql;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "来源名称");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "authTime");
		map.put(json_title, "授权时间");
		map.put("width", 150);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "attn");
		map.put(json_title, "联系人");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "position");
		map.put(json_title, "职位");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "telephone");
		map.put(json_title, "电话");
		map.put("width", 100);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("authTime", sql_date_method+"("+entity_alias_name+".authBeginTime, '"+sql_date_format+"') || '至' || "
					+ sql_date_method+"("+entity_alias_name+".authEndTime, '"+sql_date_format+"')"
					+ " as authTime ");
			gridsel.put("attn", ""+entity_alias_name+".attn as attn ");
			gridsel.put("position", ""+entity_alias_name+".position as position ");
			gridsel.put("telephone", ""+entity_alias_name+".telephone as telephone ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere)+"order by "+entity_alias_name+".title";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("authTime", sql_date_method+"("+entity_alias_name+".authBeginTime, '"+sql_date_format+"') || '至' ||"
					+ sql_date_method+"("+entity_alias_name+".authEndTime, '"+sql_date_format+"')"
					+ " as authTime ");
			gridsel.put("attn", ""+entity_alias_name+".attn as attn ");
			gridsel.put("position", ""+entity_alias_name+".position as position ");
			gridsel.put("telephone", ""+entity_alias_name+".telephone as telephone ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public NewsTeleplayRela getSourceByTitle(String name) throws Exception {
		try {
			Map<String,String> map = new HashMap<String,String> ();
			map.put("title", name);
			List<NewsTeleplayRela> sources = this.getDao().queryDataList(this.getHql(map), 0, 0);
			return  sources.size()==1?sources.get(0) : null;
		} catch (Exception e) {
			//throw new Exception("getSourceByTitle error by email=["+name+"]"+e.getMessage());
			throw new Exception("getSourceByTitle error by title=["+name+"]"+e.getMessage());
		}
	}
	@Override
	public INewsTeleplayRelaDao getDao() throws Exception {
		return newsTeleplayRelaDao;
	}
	@Override
	public void deleteNewsProgramRelalist(String userid){
		newsTeleplayRelaDao.deleteNewsProgramRelalist(userid);
	}

	public INewsTeleplayRelaDao getNewsTeleplayRelaDao() {
		return newsTeleplayRelaDao;
	}

	public void setNewsTeleplayRelaDao(INewsTeleplayRelaDao newsTeleplayRelaDao) {
		this.newsTeleplayRelaDao = newsTeleplayRelaDao;
	}
	
}
